package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.dtwlwjc.entity.TFSensorSoil;
import org.jeecg.modules.demo.dtwlwjc.entity.TFSensorWeather;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglDtsblist;
import org.jeecg.modules.demo.dtwlwjc.mapper.TFSensorSoilMapper;
import org.jeecg.modules.demo.dtwlwjc.mapper.TFSensorWeatherMapper;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglDtsblistMapper;
import org.jeecg.modules.demo.dtwlwjc.service.ITFSensorWeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TFSensorWeatherServiceImpl extends ServiceImpl<TFSensorWeatherMapper, TFSensorWeather> implements ITFSensorWeatherService {
    @Autowired
    private TFSensorWeatherMapper tfSensorWeatherMapper;
    @Autowired
    private TFSensorSoilMapper tfSensorSoilMapper;
    @Autowired
    private ZwglDtsblistMapper zwglDtsblistMapper;

    //    @Override
//    public Result transferData(Integer deviceid) {
//        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("deviceid", deviceid);
//        queryWrapper.orderByDesc("synctime");
//        queryWrapper.last("limit 1");
//        List<TFSensorWeather> list = tfSensorWeatherMapper.selectList(queryWrapper);
//        return Result.OK(list);
//
//    }
    @Override
    public Result transferData(String dkmc) {
        QueryWrapper<ZwglDtsblist> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dkmc", dkmc);
        queryWrapper1.like("sbmc","气象");
        List<ZwglDtsblist> list1 = zwglDtsblistMapper.selectList(queryWrapper1);
        Integer deviceid = 0;
        for (ZwglDtsblist zwglDtsblist : list1) {
            deviceid = Integer.valueOf(zwglDtsblist.getDkbh());
        }
        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
        queryWrapper.last("limit 1");
        List<TFSensorWeather> list = tfSensorWeatherMapper.selectList(queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result transferDataa(String dkmc) {
        QueryWrapper<ZwglDtsblist> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dkmc", dkmc);
        queryWrapper1.like("sbmc","气象");
        List<ZwglDtsblist> list1 = zwglDtsblistMapper.selectList(queryWrapper1);
        Integer deviceid = 0;
        for (ZwglDtsblist zwglDtsblist : list1) {
            deviceid = Integer.valueOf(zwglDtsblist.getDkbh());
        }
        return Result.OK(tfSensorWeatherMapper.getLatestDataForDevice(String.valueOf(deviceid)));
    }

    @Override
    public Result qixiangshijian(String starttime, String endtime, Integer deviceid, Integer pageNo) {
        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
        if (!endtime.contains(" ")) {
            endtime += " 23:59:59";
        }
        queryWrapper.between("synctime", starttime, endtime);
        queryWrapper.eq("deviceid", deviceid);
        Page<TFSensorWeather> page = new Page<>(pageNo, 10);
        Page<TFSensorWeather> list = tfSensorWeatherMapper.selectPage(page, queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result shangqingshijian(String starttime, String endtime, Integer deviceid, Integer pageNo) {
        QueryWrapper<TFSensorSoil> queryWrapper = new QueryWrapper<>();
        if (!endtime.contains(" ")) {
            endtime += " 23:59:59";
        }
        queryWrapper.between("synctime", starttime, endtime);
        queryWrapper.eq("deviceid", deviceid);
        Page<TFSensorSoil> page = new Page<>(pageNo, 10);
        Page<TFSensorSoil> list = tfSensorSoilMapper.selectPage(page, queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result transferDatb(Integer pageNo, Integer deviceid) {
        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
        Page<TFSensorWeather> page = new Page<>(pageNo, 10);
        Page<TFSensorWeather> list = tfSensorWeatherMapper.selectPage(page, queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result transferDatbb(Integer pageNo, Integer deviceid) {
        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
//        Page<TFSensorWeather> page = new Page<>(pageNo, 10);
//        Page<TFSensorWeather> list = tfSensorWeatherMapper.selectPage(page, queryWrapper);
        List<TFSensorWeather> list = tfSensorWeatherMapper.selectList(queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result transferDatc(Integer pageNo, Integer deviceid) {
        QueryWrapper<TFSensorSoil> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
        Page<TFSensorSoil> page = new Page<>(pageNo, 10);
        Page<TFSensorSoil> list = tfSensorSoilMapper.selectPage(page, queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result getsqsj(Integer deviceid) {
        LocalDate today = LocalDate.now();
        QueryWrapper<TFSensorSoil> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("synctime", LocalDateTime.now().minusDays(7)) // 创建时间大于等于7天前
                .groupBy("DATE(synctime)").orderByDesc("synctime");
        List<TFSensorSoil> resultList = tfSensorSoilMapper.selectList(queryWrapper);
        return Result.OK(resultList);

    }

    @Override
    public Result getsqsssj(Integer deviceid) {
        QueryWrapper<TFSensorSoil> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
        queryWrapper.last("limit 1");
        List<TFSensorSoil> list = tfSensorSoilMapper.selectList(queryWrapper);
        return Result.OK(list);

    }

    @Override
    public Result getqixiangshijian(String kaishishijian, String jieshushijian, Integer deviceid) {
        // 格式化开始和结束时间
        String formattedKaishishijian = kaishishijian + " 00:00:00";
        String formattedJieshushijian = jieshushijian + " 23:59:59";

        // 调用 MyBatis 映射器接口的方法
        List<TFSensorWeather> list = tfSensorWeatherMapper.selectByCondition(formattedKaishishijian, formattedJieshushijian, deviceid);

        // 返回结果
        return Result.OK(list);
    }
}
